package com.baiyangtx.lingerweather.net;

import java.util.HashMap;
import java.util.Map;

import com.baiyangtx.lingerweather.adapt.CityKey;

public class RealTimeParser implements Parser {

	private String text ;
	
	
	private Map<String , String> city ;
	
	@Override
	public void setText(String text) {
		this.text = text ;
	}

	@Override
	public Map<String, String> parser() {
		if(city == null ){
			city =  new HashMap<String , String>(); ;
			Map<String , String > map = parseToPair() ;
			String temp = map.get("temp") ;
			String wd = map.get("WD") ;
			String ws = map.get("WS") ;
			try{
				Integer.valueOf(temp) ;
			}catch(Throwable t){
				temp = "无" ;
			}
			city.put(CityKey.REAL_TEMPERATURE 	, temp ) ;
			city.put(CityKey.WIND , wd ) ;
			city.put(CityKey.WIND_POWER  , ws ) ;
		}
		return city ;
	}

	
	private Map<String , String> parseToPair(){
		
		Map<String , String> map = new HashMap<String, String>() ;
		String[] keyValue = text.split(",") ;
		for(int i = 0 ; i < keyValue.length ; i++)
		{
			String[] pair = keyValue[i].replaceAll("\"", "").split(":") ;
			if( pair.length > 1 ){
				map.put(pair[0], pair[1]) ;
			}else{
				map.put(pair[0], "null");
			}
		}
		return map ;
	}
	
	
	
	
}
